import {describe, expect, it} from "vitest";
import settings from "../js/settings";
import urlParameters from "../js/urlParameters";

describe("urlParameters", () => {
  // no parameters
  it("defaults", () => {
    let args = urlParameters("");
    expect(args).toMatchObject({
      has_query: false,
      has_coords: false,
      has_zoom: false,
      run_query: false
    });
    args = urlParameters("?");
    expect(args).toMatchObject({
      has_query: false,
      has_coords: false,
      has_zoom: false,
      run_query: false
    });
  });
  // query (uncompressed)
  it("query", () => {
    const args = urlParameters("?Q=foo");
    expect(args).toMatchObject({
      has_query: true,
      query: "foo",
      has_coords: false,
      has_zoom: false
    });
  });
  // query (compressed)
  it("query (compressed)", () => {
    const args = urlParameters("?q=Zm9v");
    expect(args).toMatchObject({
      has_query: true,
      query: "foo",
      has_coords: false,
      has_zoom: false
    });
  });
  // coords (uncompressed)
  it("coords", () => {
    const args = urlParameters("?C=0;180;1");
    expect(args).toMatchObject({
      has_query: false,
      has_coords: true,
      coords: {lat: 0.0, lng: 180.0},
      has_zoom: true,
      zoom: 1
    });
  });
  // coords (uncompressed, lat/lon/zoom)
  it("coords (lat/lon/zoom)", () => {
    let args = urlParameters("?lat=0&lon=180.0&zoom=1");
    expect(args).toMatchObject({
      has_query: false,
      has_coords: true,
      coords: {lat: 0.0, lng: 180.0},
      has_zoom: true,
      zoom: 1
    });
    args = urlParameters("?lat=0&lon=180.0");
    expect(args).toMatchObject({
      has_query: false,
      has_coords: true,
      coords: {lat: 0.0, lng: 180.0},
      has_zoom: false
    });
    args = urlParameters("?zoom=1");
    expect(args).toMatchObject({
      has_query: false,
      has_coords: false,
      has_zoom: true,
      zoom: 1
    });
  });
  // coords (compressed)
  it("coords (compressed)", () => {
    const args = urlParameters("?c=CTVpCWdRAB");
    expect(args).toMatchObject({
      has_coords: true,
      coords: {lat: 0.0, lng: 180.0},
      has_zoom: true,
      zoom: 1
    });
  });
  // RUN flag
  it("RUN flag", () => {
    let args = urlParameters("?Q=foo&R");
    expect(args).toMatchObject({run_query: true});
    args = urlParameters("?Q=foo&R=true");
    expect(args).toMatchObject({run_query: true});
  });
  // wizard (trim)
  it("wizard (trim)", () => {
    const args = urlParameters(
      "?w=addr%3Astreet%3D%22T%C4%81lival%C5%BEa%20iela%22%20in%20Riga%0A"
    );
    expect(args).toMatchObject({
      has_query: true,
      query: `/*

*/
[out:json][timeout:25];
// fetch area “Riga” to search in
{{geocodeArea:Riga}}->.searchArea;
// gather results
nwr["addr:street"="Tālivalža iela"](area.searchArea);
// print results
out geom;`,
      has_coords: false,
      has_zoom: false
    });
  });
  // template
  it("template", async () => {
    const orig_ss = settings.saves;
    settings.saves = {
      T: {type: "template", parameters: ["p"], wizard: "name={{p}}"}
    };
    const args = new Promise((resolve, reject) =>
      urlParameters(
        "?template=T&p=foo",
        (err, result) => (err && reject(err)) || resolve(result)
      )
    );
    await expect(args).resolves.toMatchObject({
      has_query: true,
      query: `/*
This has been generated by the overpass-turbo wizard.
The original search was:
“name=foo”
*/
[out:json][timeout:25];
// gather results
nwr["name"="foo"]({{bbox}});
// print results
out geom;`
    });
    settings.saves = orig_ss;
  });
});
